package com.doradance.view.recyclerview;

import android.content.Context;
import android.os.SystemClock;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.doradance.R;
import com.doradance.net.ServerField;
import com.doradance.view.listener.OnLoadMoreListener;
import com.yong.android.framework.view.adapter.ViewHolderLifecycle;
import com.yong.android.framework.view.recyclerview.CellListAdapter;
import com.yong.android.framework.view.recyclerview.LoadingMoreCell;


/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

final class DefaultLoadMoreCell extends LoadingMoreCell<DefaultLoadMoreCell.LoadMoreHolder> {
    private static final String TAG = "LoadMore";

    static final int STATUS_NONE = 0;
    static final int STATUS_IDLE = 1;
    static final int STATUS_REFRESH = 2;
    static final int STATUS_NO_MORE = 3;
    static final int STATUS_FAILURE = 4;
    private int page = INIT_PAGE;
    /**
     * 根据接口第一页对应的数字
     */
    static final int INIT_PAGE = ServerField.SERVER_FIRST_INDEX;
    final int pageSize = ServerField.SERVER_PAGE_SIZE_DEFAULT;
    private int mStatus = STATUS_NONE;
    private OnLoadMoreListener onLoadMoreListener;
    private int oldPosition = -1;


    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    @Override
    protected LoadMoreHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        LoadMoreHolder loadMoreHolder = new LoadMoreHolder(onLoadMoreListener,inflater, parent);
        loadMoreHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mStatus == STATUS_FAILURE && onLoadMoreListener != null) {
                    onLoadMoreListener.onLoadMore();
                }
            }
        });
        return new LoadMoreHolder(onLoadMoreListener,inflater, parent);
    }

    @Override
    protected void onBindViewHolder(LoadMoreHolder holder) {
        holder.updateStatus(mStatus);
    }


    @Override
    protected void onVisible(LoadMoreHolder holder) {
        if (!isEnable()) {
            mStatus = STATUS_NONE;

        } else {
            if (mStatus == STATUS_NONE || mStatus == STATUS_IDLE) {
                notifyRefresh();
            }
        }

    }


    private void notifyRefresh() {
        if (onLoadMoreListener != null) {
            onLoadMoreListener.onLoadMore();
            mStatus = STATUS_REFRESH;
            getAdapter().notifyItemChanged(getAdapter().getItemCount() - 1);
        }
    }

    public void notifyFailure() {
        if (isEnable()) {
            mStatus = STATUS_FAILURE;
            getAdapter().notifyItemChanged(getAdapter().getItemCount() - 1);
        }
    }

    public int getPage() {
        return page;
    }

    public void init() {
        this.page = INIT_PAGE;
    }

    public int getPageSize() {
        return pageSize;
    }

    /**
     * 更新之前请先确定prepare
     */
    public void notifySuccess() {
        CellListAdapter adapter = getAdapter();
        int newCount = adapter.getTotalCount();
        if (page == INIT_PAGE && newCount == 0) {
            setEnable(false);
            return;
        }
        setEnable(true);
        int loadSize = newCount - oldPosition;
        if (loadSize < pageSize) {
            mStatus = STATUS_NO_MORE;
        } else {
            mStatus = STATUS_IDLE;
        }
        if (page == INIT_PAGE) {
            adapter.notifyDataSetChanged();
        } else {
            if (loadSize != 0) {
                adapter.notifyItemRangeInserted(oldPosition, loadSize);
                adapter.notifyItemRangeChanged(oldPosition, loadSize);
                adapter.notifyItemChanged(adapter.getItemCount() - 1);
            } else {
                adapter.notifyItemChanged(newCount);
            }
        }

        page++;

    }

    /**
     * 装载更多视图之前准备数据
     */
    public void prepare() {
        if (page == INIT_PAGE) {
            setEnable(false);
            oldPosition = getAdapter().getItemCount();
        } else {
            oldPosition = getAdapter().getItemCount() - 1;
        }

    }


    static class LoadMoreHolder extends RecyclerView.ViewHolder implements ViewHolderLifecycle {
        private ProgressBar ivLoading;
        private TextView tvStatus;
        private int mStatus;
        private Context mContext;
        OnLoadMoreListener mOnLoadMoreListener;
        public LoadMoreHolder(OnLoadMoreListener onLoadMoreListener,LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.layout_load_more_default, parent, false));
            mOnLoadMoreListener = onLoadMoreListener;
            mContext = parent.getContext();
            ivLoading = (ProgressBar) itemView.findViewById(R.id.progressBar);
            tvStatus = (TextView) itemView.findViewById(R.id.tv_loading_status);

        }

        public void updateStatus(int status) {
            this.mStatus = status;
            if (mStatus == STATUS_NONE) {
                ivLoading.setVisibility(View.INVISIBLE);
                tvStatus.setVisibility(View.INVISIBLE);
            } else if (mStatus == STATUS_IDLE) {
                ivLoading.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvStatus.setText(itemView.getContext().getString(R.string.tip_load_more_idle));
            } else if (mStatus == STATUS_REFRESH) {
                ivLoading.setVisibility(View.VISIBLE);
                tvStatus.setVisibility(View.VISIBLE);
                tvStatus.setText(itemView.getContext().getString(R.string.tip_load_more_refresh));
            } else if (mStatus == STATUS_NO_MORE) {
                ivLoading.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvStatus.setText(itemView.getContext().getString(R.string.tip_load_more_no_more));
            } else if (mStatus == STATUS_FAILURE) {
                ivLoading.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvStatus.setText(itemView.getContext().getString(R.string.tip_load_more_failure));
                tvStatus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvStatus.setText("正在加载更多");
                        ivLoading.setVisibility(View.VISIBLE);
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                SystemClock.sleep(1000);
                                mOnLoadMoreListener.onLoadMore();
                            }
                        }).start();

                    }
                });
            }
        }

        @Override
        public void onViewAttachedToWindow() {
//            if (mStatus == STATUS_REFRESH) {
//                mProgressDrawable.setProgressRotation(0.8f);
//                mProgressDrawable.setStartEndTrim(0f, 0.5f);
//                mProgressDrawable.setAlpha(255);
//                mProgressDrawable.start();
//                ivLoading.setVisibility(View.VISIBLE);
//            }
        }

        @Override
        public void onViewDetachedFromWindow() {
            if (mStatus == STATUS_REFRESH) {
                ivLoading.setVisibility(View.GONE);
            }
        }
    }
}
